<?php

/**
 * 摩宝支付单申报服务
 * @author chenshubo
 */
namespace App\Service\VendorClient\OrderPayDeclareService\BmDeclareServiceApi;
use App\Service\Code\Normal;
use App\Service\BaseService;
use Service\AppService\IdService;
use App\Service\TaskServer\TaskWorker;

class BmDeclareServiceApiClient extends BaseService
{
    /**
     * 申报接口
     * @param array $params
     * @return mixed
     */
    public function OrderApply( $param = [] )
    {
        $orderInfo      = $param['supplierOrder'];
        $order          = $param['order'];
        $pay_time       = date('Y-m-d H:i:s',time());
        $pay_sn         = IdService::instance()->getOtherID();
        $data = [
            'payEnterpriseTypeEnum' => 1,
            'payMentInfo' => [
                'merchOrderNo'  => (string)$pay_sn,
                'orderNo'       => $orderInfo['supplier_dsn'],
                'payAmt'        => $orderInfo['real_total_money'],
                'payerIdNumber' => $orderInfo['buyer_idcard'],
                'payerName'     => $orderInfo['buyer_realname'],
                'telephone'     => $orderInfo['buyer_phone'],
                'payTime'       => $pay_time
            ]
        ];
        $requestParam = [
            'method'    => 'apply_url',
            'data'      => $data,
        ];
        return $this->execute($requestParam);
    }

    /**
     * CURL请求
     * @param $params
     * @return mixed
     */
    public function execute( $param = [] )  
    {
        return Normal::returnCode(Normal::FAIL,"不支持的支付方式MOB，请联系相应的业务人员");
        
        $header = [
            "Content-Type: application/json",
            "Accept:application/json"
        ];
        
        //获取福州关务通知接口地址
        $declareInfo = config("api.mobao.acount");
        if( empty($declareInfo) ) {
            return Normal::returnCode(Normal::FAIL,"申报接口信息不能为空");
        }
        
        if( empty($declareInfo[$param['method']]) ) {
            return Normal::returnCode(Normal::FAIL,"申报接口地址不能为空");
        }
        
        $applyUrl = $declareInfo['apply_url'];
        $data = $param['data'];
        $data['microServiceName'] = $declareInfo['microServiceName'];
        $data['payMentInfo'] = array_merge($data['payMentInfo'],[
            'ebpName'       => $declareInfo['ebpName'],
            'merchNo'       => $declareInfo['merchNo'],
            'customsType'   => $declareInfo['customsType'],
            'merchkey'      => $declareInfo['merchkey'],
        ]);
        
        $parameter = [
            [
                'tag'       => 'mobaoDecalre',
                'uid'       => IdService::instance()->getOtherID(),
                'url'       => $applyUrl,
                'data'      => $data,
                'method'    => 'post',
                'format'    => 'json',
                'header'    => [],
            ],
        ];
        
        $resTask = TaskWorker::instance()->addTask($parameter);
        $resTask = current($resTask);
        $result  = @json_decode($resTask['response']['result'],true);
        $httpCode = isset($result['declareResult']) && $result['declareResult'] == true ? 200 : 0;
        $msg = !empty($result) ? ($result['message']??'创建失败') : $resTask['response']['result'] ;
        return Normal::returnCode($httpCode,$msg);
    }
}
